package com.common.domainService;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.applicationLayer.formbean.StepForm;
import com.common.DAO.IStepDAO;
import com.common.DAO.ITownDAO;
import com.common.entity.Step;
import com.common.transformer.StepTransformer;

public class StepService implements IStepService  {
	Logger logger = Logger.getLogger(PathService.class);
	private IStepDAO stepDAO;
	private ITownDAO townDAO;

	@Override
	public void createSteps(List<StepForm> steps, int id_path){
		int ordre = 1;
		this.getStepDAO().removeStepByPath(id_path);
		for(StepForm stepForm : steps){
			try{
				Step step = StepTransformer.StepForm_TO_Step(stepForm);
				step.setPath(id_path);
				step.setOrder(ordre);
				this.getStepDAO().addStep(step);
			}catch (Exception e) {
				logger.error("An error has occured in PathService while adding", e);
			}
			ordre++;
		}
	}

	public IStepDAO getStepDAO() {
		return stepDAO;
	}

	public void setStepDAO(IStepDAO stepDAO) {
		this.stepDAO = stepDAO;
	}

	public ITownDAO getTownDAO() {
		return townDAO;
	}

	public void setTownDAO(ITownDAO townDAO) {
		this.townDAO = townDAO;
	}

	@Override
	public List<StepForm> getStepsByPath(int id_path) {
		List<Step> steps = this.stepDAO.listStepsByPath(id_path);
		List<StepForm> stepForms = new ArrayList<StepForm>();
		for(Step step : steps){
			StepForm stepForm = StepTransformer.Step_TO_StepForm(step);
			stepForm.setTownName(this.getTownDAO().getTownByid(step.getTown()).getName());
			stepForms.add(stepForm);
		}
		return stepForms;
	}

}
